Attempt at Merge Sort: Is this correct? [migrated]

Posted by Beatrice on Programmers See other posts from Programmers or by Beatrice
Published on 2012-10-16T20:47:10Z Indexed on 2012/10/16 23:19 UTC
Read the original article Hit count: 83

Filed under:
|

I am trying to write a merge sort algo. I can't tell if this is actually a canonical merge sort. If I knew how to calculate the runtime I would give that a go. Does anyone have any pointers? Thanks.

public static void main(String[] argsv) {

    int[] A = {2, 4, 5, 7, 1, 2, 3, 6};
    int[] L, R;

    L = new int[A.length/2];
    R = new int[A.length/2];

    int i = 0, j = 0, k;

    for (k = 0; k < A.length; k++) {
        if (k < A.length/2) {
            L[i] = A[k];
            i++;
        }
        else {
            R[j] = A[k];
            j++;
        }
    }

    i = 0;
    j = 0;

    for (k = 0; k < A.length; k++) {
        System.out.println(i + " " + j + " " + k);
        if (i < L.length && j < R.length) {
            if (L[i] < R[j]) {
                A[k] = L[i];
                i++;
            }
            else {
                A[k] = R[j];
                j++;
            }
        }
    }
}    

© Programmers or respective owner

Related posts about algorithms

Related posts about sorting